package conversion.service.impl;



import conversion.service.EmailService;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Random;

@Service
public class EmailServiceImpl implements EmailService {

    @Resource
    private JavaMailSender mailSender;

    public void sendEmail(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置发件人地址
        message.setFrom("1151298644@qq.com");
        // 设置收件人地址
        message.setTo(to);
        // 设置邮件标题
        message.setSubject(subject);
        // 设置邮件内容
        message.setText(text);
        mailSender.send(message);
    }

    public  String getVerificationCode() {
        LocalDateTime currentTime = LocalDateTime.now();

        // 使用当前时间的毫秒数作为种子
        long seed = currentTime.getNano();

        Random random = new Random(seed);

        // 生成六位随机数
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            int digit = random.nextInt(10); // 生成0到9的随机数
            sb.append(digit);
        }

        return sb.toString();
    }
}